package io.zhaocy;

import com.alibaba.edas.acm.ConfigService;
import com.alibaba.edas.acm.exception.ConfigException;
import com.alibaba.edas.acm.listener.ConfigChangeListener;

import java.util.Properties;

/**
 * Hello world!
 *
 */
public class App 
{

    // Properties/Switch
    private static String config = "DefaultValue";

    private static Properties acmProperties = new Properties();

    public static void main(String[] args) {
        try {

            // Initialize configuration service and the console will retrieve the following parameters through the sample code. Input parameters include endpoint, namespace, accessKey, and secretKey(The secrectKey of ACM. Do not use the secrectKey of your Alibaba acount.).
            Properties properties = new Properties();
            properties.put("endpoint", "acm.aliyun.com");
            properties.put("namespace", "1bf1e152-6f01-4456-abe7-f689478328a8");
            properties.put("accessKey", "LTAITC0he2Y6eMJ7");
            properties.put("secretKey", "FteizfsnIZyn0Sq90PQNYxgeZTDr2s");
//            properties.put("openKMSFilter", true);
//            properties.put("regionId", "cn-shanghai");
//            properties.put("ramRoleName", "AliyunACMAccessingKMSRole");

            ConfigService.init(properties);

            Properties global = ConfigService.getConfig2Properties("com.snowball.cloud:global.properties", "DEFAULT_GROUP", 3000);
            System.out.println(global.toString());
            // Get configuration proactively
            Properties appConf = ConfigService.getConfig2Properties("com.snowball.cloud:acm-app.properties", "DEFAULT_GROUP", 3000);
            System.out.println(appConf.toString());

            Properties merge = new Properties();
            merge.putAll(appConf);
            merge.putAll(global);

            System.out.println(merge.toString());

//            System.out.println(appConf.toString());

            // Add listener for the configuration during initialization, so that configuration changes will trigger callback notifications.
            ConfigService.addListener("com.snowball.cloud:acm-app.properties", "DEFAULT_GROUP", new ConfigChangeListener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    config = configInfo;
                    System.out.println(configInfo);
                }
            });

        } catch (ConfigException e) {
            e.printStackTrace();
        }

        // In this sample, the main thread does not exit, because the configuration subscription is the daemon thread, and it will exit if the main threads exits. The following code is not needed in real scenarios.
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // Expose the configuration value with the GET interface
    public static String getConfig() {
        return config;
    }

    // Expose the configuration value with the GET interface
    public static Object getPorpertiesValue(String key) {
        if (acmProperties != null) {
            return acmProperties.get(key);
        }
        return null;
    }

}
